// pages/classDetail/classDetail.js
let api = require("../../api/api.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: 0, // 0-详情, 1-评论
    showBuy: false, // 展示买弹窗
    courseDetial:{}, // 
    userEvaluate:[], // 用户评价
    isBuy: false,
    userFavList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let index = options.course_id; //获取课程详情
    let that = this;
    api.courseDetial(index).then(res=>{
      console.log(res)
      that.setData({ courseDetial: res.data })
      that.setData({ userEvaluate: res.data.userEvaluate })
      wx.hideLoading()

      api.userHasCourse(that.data.courseDetial.id).then(res=>{
        that.setData({ isBuy: res.data.hasCourse })
      })

      that.setData({ userFavList: wx.getStorageSync('userFav') })
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onShow:function(){

  },
  
  changTab: function (e) {
    const detail = Number(e.currentTarget.dataset.value)
    this.setData({
      detail: detail
    })
  },

  /**
   * (添加/删除)收藏
   */
  fav:function(){
    let course_id = this.data.courseDetial.id
    let userFavList = wx.getStorageSync("userFav")
    let has = false;
    let index = -1;
    for(let i = 0; i < userFavList.length; i++){
      if(userFavList[i].id == course_id){
        has = true;
        index = i
        break;
      }
    }
    if(has){
      api.removeUserFav(this.data.courseDetial.id).then(res=>{
        console.log(res)
        wx.showToast({
          title: '移除收藏',
          icon: "none",
          duration: 2000
        })
        userFavList.splice(index, 1)
        wx.setStorageSync('userFav', userFavList)
        wx.hideLoading()
      })
    }else{
      api.addUserFav(this.data.courseDetial.id).then(res=>{
        console.log(res)
        wx.showToast({
          title: '收藏成功',
          icon: "none",
          duration: 2000
        })
        userFavList.push(this.data.courseDetial)
        wx.setStorageSync('userFav', userFavList)
        wx.hideLoading()
      })
    }

    this.setData({ userFavList: userFavList })
    
  },

  /**
   * 加入购物车的框框
   */
  immBuy: function() {
    this.setData({
      showBuy: true,
    })
  },
  closeBuy: function() {
    this.setData({
      showBuy: false,
    })
  },
  /**
   * 加入购物车
   */
  addShoppingCart:function(){

    api.addShoppingCart(this.data.courseDetial.id).then(res=>{
      console.log(res)
      if(!this.data.isBuy){

        if(res.data.code != 500){
          wx.hideLoading()
          wx.showToast({
            title: '添加成功',
            icon: 'none',
            duration: 2000
          })
        }else if(res.data.code == 500){
          //已经存在购物车
          wx.hideLoading()
          wx.showToast({
            title: '课程已存在哦',
            icon: 'none',
            duration: 2000
          })
        }

      }else{

        wx.showToast({
          title: '课程已经购买',
        })

      }

    })

  },

  singleBuy:function(){
    let userInfo = wx.getStorageSync('userInfo')
    let course_id = this.data.courseDetial.id
    if(!this.data.isBuy){

      api.singlePay(userInfo.mobile, course_id).then(res=>{
        wx.hideLoading()
        let orderInfo = res.data
        console.log(orderInfo)
        api.startPay(orderInfo)
      })

    }else{
      wx.showToast({
        title: '课程已经购买了',
      })
    }
  },

  toTimeLine:function(){
    console.log(this.data.courseDetial)
    let timeLine = this.data.courseDetial.courseTimeLine
    let _timeLine = JSON.stringify(timeLine)
    wx.navigateTo({
      url: '/pages/courseTimeLine/courseTimeLine?timeLine='+_timeLine,
    })
  }
})